package chapter04;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestCorrectPostureStep4 {
    private static final Object ROOM=new Object();
    private static boolean hasCigarette=false;
    private static boolean hasTaskOut=false;
    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            synchronized (ROOM){
                log.debug("有烟没？【{}】",hasCigarette);
                while (!hasCigarette){
                    log.debug("没有烟，歇一会");
                    try {
                        ROOM.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("有烟没？【{}】",hasCigarette);
                if (hasCigarette){
                    log.debug("有烟了，开始干活");
                }else {
                    log.debug("没烟，不干了");
                }
            }
        },"小南").start();

        new Thread(()->{
            synchronized (ROOM){
                log.debug("有外买没？【{}】",hasTaskOut);
                while (!hasTaskOut){
                    log.debug("没有外买，歇一会");
                    try {
                        ROOM.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("有外买没？【{}】",hasTaskOut);
                if (hasTaskOut){
                    log.debug("有外买了，开始干活");
                }
            }
        },"小女").start();

        Thread.sleep(1000);
        new Thread(()->{
            synchronized (ROOM){
                log.debug("外面到了哦~");
                hasTaskOut=true;
                ROOM.notifyAll();
            }
        },"送烟的人").start();
    }
}
